# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


def load_data_set():
    data_mat = []
    label_mat = []
    fr = open('test_set.txt', 'r')
    for line in fr.readlines() :
        line_arr = line.strip().split()
        data_mat.append([1.0, float(line_arr[0]), float(line_arr[1])])
        label_mat.append(int(line_arr[2]))
    fr.close()
    return data_mat, label_mat

#----------------------------------------------------------------------
def sigmoid(in_x):
    """S型的意思"""
    return 1.0/(1 + np.exp(-in_x))

#----------------------------------------------------------------------
def grad_ascent(data_mat_in, class_lables):
    """"""
    data_matrix = np.mat(data_mat_in)
    #print('class_lables',np.mat(class_lables).shape)
    label_mat = np.mat(class_lables).transpose()   # class_lables为list，mat后为1行100列的matrix，‘T’一般用于1,2维数组，高维的用transpose,
    #print('label_mat',label_mat.shape)
    m,n = np.shape(data_matrix)
    alpha = 0.001              # 
    max_cycles = 5
    weights = np.ones((n, 1))
    for k in range(max_cycles):
        h = sigmoid(data_matrix * weights)          # 得到100行1列的数据，经过S型函数处理的，但是为什么100行3列要变成100行1列
        error = (label_mat - h)
        weights = weights + alpha * data_matrix.transpose() * error
    return weights


#----------------------------------------------------------------------
def stoc_grad_ascent(data_mat_in, class_lables):
    """"""
    #data_matrix = np.mat(data_mat_in)
    #label_mat = np.mat(class_lables).transpose()   # class_lables为list，mat后为1行100列的matrix，‘T’一般用于1,2维数组，高维的用transpose,
    m,n = np.shape(data_mat_in)
    alpha = 0.01              # 
    max_cycles = 5
    weights = np.ones((n, 1))
    for i in range(m):
        h = sigmoid(data_mat_in[i] * weights)          # 得到100行1列的数据，经过S型函数处理的，但是为什么100行3列要变成100行1列
        error = (class_lables[i] - h)
        weights = weights + alpha * error*data_mat_in[i]
    return weights
    
    
 
#----------------------------------------------------------------------
def plot_best_fit(weights):
    """"""
    data_mat, label_mat = load_data_set()
    data_arr = np.array(data_mat)
    n = np.shape(data_arr)[0]
    x_cord1 = []
    y_cord1 = []
    x_cord2 = []
    y_cord2 = []
    for i in range(n):
        if int(label_mat[i]) ==1:
            x_cord1.append(data_arr[i, 1])
            y_cord1.append(data_arr[i, 2])
        else:
            x_cord2.append(data_arr[i, 1])
            y_cord2.append(data_arr[i, 2])
    fig = plt.figure()
    ax = fig.add_subplot(111)       # 1*1的子画布的第一块
    ax.scatter(x_cord1, y_cord1, s=30, c='red', marker='s')
    ax.scatter(x_cord2, y_cord2, s=30, c='green')
    x = np.arange(-3.0, 3.0, 0.1)
    y = (-weights[0] - weights[1]*x)/weights[2]
    x = x.reshape(1, 60)
    b = x.reshape(1, 60)
    y = np.array(y)
    ax.plot(x,y,'r*')
    plt.xlabel('X1')
    plt.ylabel('X2')
    plt.show()
            
            
        
    
data_arr, label_mat = load_data_set()
weights = grad_ascent(data_arr, label_mat)
sss = plot_best_fit(weights) 
print(sss)  

    